package com.archermind.style.controller;

import com.alibaba.fastjson.JSONObject;
import com.archermind.style.bean.Instance;
import com.archermind.style.interceptor.Auth;
import com.archermind.style.service.InstanceService;
import com.archermind.style.service.OperationService;
import com.archermind.style.util.Result;
import com.archermind.style.util.Search;
import com.archermind.style.util.SessionUtil;
import com.feng.fengDUtil.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@RestController
@RequestMapping(value = "/instance")
public class InstanceAction {

    @Autowired
    private InstanceService instanceService;

    @Autowired
    private OperationService opService;

    @RequestMapping(value = "/list")
    public Result listIconType( Page page,Search search) {

        instanceService.query(page,search);
        return Result.success(page);
    }



    @RequestMapping(value="/del")
    public Result del(String ids){
        try {
            opService.saveLog("删除了样式："+instanceService.getInstanceName(Integer.parseInt(ids)),Integer.parseInt(ids));
            instanceService.delInstances(ids);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/update")
    public Result save(@RequestBody Instance instance){
        try {
            instance.setModifier(SessionUtil.getCurUserId());
            instance.setModifyTime(new Date());
            opService.saveLog("编辑了样式："+instanceService.getInstanceName(instance.getId()),instance.getId());
            instanceService.update(instance);
            return Result.success(instance);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/refreshInstanceIcon")
    public Result refreshInstanceIcon(@RequestParam Integer instanceId){
        try {
            instanceService.refreshInstanceIcon(instanceId);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @Auth("复制样式")
    @RequestMapping(value="/copy")
    public Result copy(@RequestBody Instance instance){
        try {
            instanceService.copyInstance(instance);

            return Result.success(instance);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/checkUniqueName_mute")
    public JSONObject checkUniqueName(@RequestParam(value = "id",required = false) Long id, @RequestParam(value = "name",required = true) String name){
        JSONObject ret = new JSONObject();
        ret.put("isUnique",instanceService.isUnique(id,name));
        return ret;
    }


    @Auth("导入样式")
    @RequestMapping(value = "/uploadXml", method = RequestMethod.POST)
    public Result uploadXml(MultipartFile file,Instance instance ){
        String name = file.getOriginalFilename();
        try {
            Result conflicts = instanceService.processXml(file, instance);
            if (conflicts != null) return conflicts;

         /*   BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line+"\n");
            }sb.toString();*/

         /*  Map map = XMLUtil.doXMLParse(sb.toString());*/


            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }



    @RequestMapping(value="/test")
    public Result test(){
        try {
            instanceService.test();
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


}